package com.bytedance.scenedemo;

import com.bytedance.scene.Scene;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scenedemo.ability_compatibility.ability_result.SceneGetAbilityResultSample;
import com.bytedance.scenedemo.ability_compatibility.configuration.ConfigurationDemoScene;
import com.bytedance.scenedemo.ability_compatibility.scene_result.AbilityGetSceneResultSampleAbility;
import com.bytedance.scenedemo.ability_compatibility.softkeyboard.SoftKeyboardDemoScene;
import com.bytedance.scenedemo.ability_compatibility.theme.ThemeDemo;
import com.bytedance.scenedemo.ability_compatibility.window.WindowDemo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;

public class AbilityCompatibilitySamples extends Scene implements Component.ClickedListener {
    private static final String WRITE_USER_STORAGE = "ohos.permission.WRITE_USER_STORAGE";

    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component parse = scatter.parse(ResourceTable.Layout_fraction_compatibility_samples, container, false);
        Text text = (Text) parse.findComponentById(ResourceTable.Id_text_navigation_list_hint);
        text.setText(getClassName());

        initListener(parse);
        return parse;
    }

    private void initListener(Component parse) {
        Button sceneGetAbilityResult = (Button) parse.findComponentById(ResourceTable.Id_button_scene_get_ability_result);
        sceneGetAbilityResult.setClickedListener(this);

        Button abilityGetSceneResult = (Button) parse.findComponentById(ResourceTable.Id_button_ability_get_scene_result);
        abilityGetSceneResult.setClickedListener(this);

        Button navResultPermission = (Button) parse.findComponentById(ResourceTable.Id_button_nav_result_permission);
        navResultPermission.setClickedListener(this);

        Button configurationChange = (Button) parse.findComponentById(ResourceTable.Id_button_configuration_change);
        configurationChange.setClickedListener(this);

        Button ime = (Button) parse.findComponentById(ResourceTable.Id_button_ime);
        ime.setClickedListener(this);
        Button theme = (Button) parse.findComponentById(ResourceTable.Id_button_theme);
        theme.setClickedListener(this);
        Button modifyAbilityStates = (Button) parse.findComponentById(ResourceTable.Id_button_modify_ability_states);
        modifyAbilityStates.setClickedListener(this);

    }

    private void JumpIntent(Class cls) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getAbility().getBundleName())
                .withAbilityName(cls)
                .build();
        intent.setOperation(operation);
        SceneExtensions.requireNavigationScene(this).startAbility(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_scene_get_ability_result:
                SceneExtensions.requireNavigationScene(this).push(SceneGetAbilityResultSample.class);
                System.out.println("TAG-----sceneGetAbilityResult");
                break;
            case ResourceTable.Id_button_ability_get_scene_result:
                JumpIntent(AbilityGetSceneResultSampleAbility.class);
                System.out.println("TAG-----abilityGetSceneResult");
                break;
            case ResourceTable.Id_button_nav_result_permission:
                SceneExtensions.requireNavigationScene(this).requestPermissions(new String[]{WRITE_USER_STORAGE}, 123, grantResults -> {
                    if (grantResults != null && grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                        new ToastDialog(getAbility()).setText(getString(ResourceTable.String_nav_result_permission_tip_success)).setDuration(2000).show();
                        System.out.println("TAG-----Permissions-success");
                    } else {
                        new ToastDialog(getAbility()).setText(getString(ResourceTable.String_nav_result_permission_tip_failed)).setDuration(2000).show();
                        System.out.println("TAG-----Permissions-failed");
                    }
                });
                System.out.println("TAG-----Permissions");
                break;
            case ResourceTable.Id_button_configuration_change:
                SceneExtensions.requireNavigationScene(this).push(ConfigurationDemoScene.class);
                System.out.println("TAG-----configurationChange");
                break;
            case ResourceTable.Id_button_ime:
                SceneExtensions.requireNavigationScene(this).push(SoftKeyboardDemoScene.class);
                System.out.println("TAG-----ime");
                break;
            case ResourceTable.Id_button_theme:
                SceneExtensions.requireNavigationScene(this).push(ThemeDemo.class);
                System.out.println("TAG-----theme");
                break;
            case ResourceTable.Id_button_modify_ability_states:
                SceneExtensions.requireNavigationScene(this).push(WindowDemo.class);
                System.out.println("TAG-----modify_ability_states");
                break;

            default:
                break;
        }
    }
}
